import { z } from 'zod'

export const tagZodFormData = z.object({
  name: z.string().max(4, "name字段长度不能超过4"),
  sign: z.string().emoji("sign必须是emoji表情"),
  kind: z.enum(['expenses', 'income'], { errorMap: () => ({ message: "kind 必须从 ['expenses', 'income']中枚举"}) })
});

export const tagGroupZodFormData = z.object({
  page: z.number().int().positive("page必须是正整数"),
  kind: z.enum(['expenses', 'income'], { errorMap: () => ({ message: "kind 必须从 ['expenses', 'income']中枚举"}) })
});

export const itemZodFormData = z.object({
  amount: z.number().int().positive("amount必须是正整数"),
  tag_id: z.number().int().positive("tag_id必须是正整数"),
  happen_at: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
  kind: z.enum(['expenses', 'income'], { errorMap: () => ({ message: "kind 必须从 ['expenses', 'income']中枚举"}) })
});

export const itemDeleteZodFormData = z.object({
  id: z.number().int().positive("id必须是正整数"),
}), tagGetZodFormData = z.object({
  id: z.number().int().positive("id必须是正整数"),
});

export const itemListParamZod = z.object({
  happened_after: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
  happened_before: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
  page: z.number().int().positive("page必须是正整数"),
}).refine(data => new Date(data.happened_after) <= new Date(data.happened_before), {
  message: "「开始时间」必须早于「结束时间」",
})

export const itemSummaryParamZod = z.object({
  happened_after: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
  happened_before: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
  // group_by: z.string(),
  group_by: z.enum(['tag_id', 'happen_at'], { errorMap: () => ({ message: "kind 必须从 ['tag_id', 'happen_at']中枚举"}) }),
  kind: z.enum(['expenses', 'income'], { errorMap: () => ({ message: "kind 必须从 ['expenses', 'income']中枚举"}) })
}).refine(data => new Date(data.happened_after) <= new Date(data.happened_before), {
  message: "「开始时间」必须早于「结束时间」",
})

export const balanceParamZod = z.object({
  happened_after: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
  happened_before: z.string()
  .refine(val => !isNaN(Date.parse(val)), {
    message: "时间参数类型错误"
  }),
}).refine(data => new Date(data.happened_after) <= new Date(data.happened_before), {
  message: "「开始时间」必须早于「结束时间」",
})

export { z }